<template>
  <ProLayout
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :i18nRender="i18nRender"
    v-bind="settings"
  >
    <template v-slot:menuHeaderRender>
      <LogoSvg />
      <h1>{{ $Conf.title }}</h1>
    </template>

    <template v-slot:headerContentRender>
      <div>
        <a-tooltip title="刷新页面">
          <a-icon
            type="reload"
            style="font-size: 18px; cursor: pointer"
            @click="handleReload"
          />
        </a-tooltip>
      </div>
    </template>

    <transition v-if="settings.multiTab">
      <multi-tab></multi-tab>
    </transition>

    <setting-drawer
      v-if="settings.showSettings"
      :settings="settings"
      @change="handleSettingChange"
    ></setting-drawer>

    <template v-slot:rightContentRender>
      <right-content
        :top-menu="settings.layout === 'topmenu'"
        :is-mobile="isMobile"
        :theme="settings.theme"
      />
    </template>

    <router-view />

    <template v-slot:footerRender><global-footer /></template>
  </ProLayout>
</template>

<script>
import ProLayout, {
  SettingDrawer,
  updateTheme,
} from "@ant-design-vue/pro-layout";
import { i18nRender } from "@/locales";
import {
  SIDEBAR_TYPE,
  TOGGLE_MOBILE_TYPE,
  CONTENT_WIDTH_TYPE,
} from "@/store/mutation-types";
import RightContent from "../GlobalHeader/RightContent";
import GlobalFooter from "../GlobalFooter";
import { LogoSvg } from "@/libs/icons";
import MultiTab from "../MultiTab/MultiTab";
import defaultSettings from "@/config/defaultSettings";

export default {
  name: "BasicLayout",
  components: {
    SettingDrawer,
    RightContent,
    GlobalFooter,
    ProLayout,
    MultiTab,
    LogoSvg,
  },
  data() {
    return {
      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      // 媒体查询
      query: {},
      // 是否手机模式
      isMobile: false,

      settings: {
        ...defaultSettings,

        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,

        contentWidth:
          defaultSettings.layout === "sidemenu"
            ? CONTENT_WIDTH_TYPE.Fluid
            : defaultSettings.contentWidth,
      },
    };
  },
  computed: {
    mainMenu: function () {
      return this.$store.getters.asyncRoutes;
    },
  },
  created() {
    const routes = this.mainMenu.find((item) => item.path === "/");
    this.menus = (routes && routes.children) || [];
    // 处理侧栏收起状态
    this.$watch("collapsed", () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed);
    });
    this.$watch("isMobile", () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile);
    });
  },
  mounted() {
    const userAgent = navigator.userAgent;
    if (userAgent.indexOf("Edge") > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed;
        setTimeout(() => {
          this.collapsed = !this.collapsed;
        }, 16);
      });
    }

    // if (
    //   process.env.NODE_ENV !== "production" ||
    //   process.env.VUE_APP_PREVIEW === "true"
    // ) {
    //   updateTheme(this.settings.primaryColor);
    // }
  },
  methods: {
    i18nRender,
    handleMediaQuery(val) {
      this.query = val;
      if (this.isMobile && !val["screen-xs"]) {
        this.isMobile = false;
        return;
      }
      if (!this.isMobile && val["screen-xs"]) {
        this.isMobile = true;
        this.collapsed = false;
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid;
        // this.settings.fixSiderbar = false;
      }
    },
    handleCollapse(val) {
      this.collapsed = val;
    },
    handleSettingChange({ type, value }) {
      type && (this.settings[type] = value);
      switch (type) {
        case "contentWidth":
          this.settings[type] = value;
          break;
        case "layout":
          if (value === "sidemenu") {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid;
          } else {
            this.settings.fixSiderbar = false;
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed;
          }
          break;
      }
    },
    handleReload() {
      this.$message.info("只是一个DEMO");
    },
  },
};
</script>

<style lang="less">
@import "./BasicLayout.less";
</style>